package com.li.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.li.pojo.dto.CourseDTO;
import com.li.pojo.po.Course;
import com.li.pojo.po.PageResult;
import com.li.pojo.po.Result;
import com.li.pojo.vo.CourseVO;
import com.li.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;


@RestController
@RequestMapping("/clues/course")
@Slf4j
public class CourseController {
    @Autowired
    private CourseService courseService;

    /**
     * 添加课程
     * @param courseDTO
     * @return
     */
    @PostMapping
    public Result addCourse(@RequestBody CourseDTO courseDTO){
        log.info("添加课程：{}",courseDTO);
        courseService.addCourse(courseDTO);
        return Result.success();
    }

    /**
     * 分页查询
     * @param courseVO
     * @return
     */
    @GetMapping("/list")
    public PageResult getList(CourseVO courseVO){
        log.info(courseVO.getParams().toString());
        return courseService.getList(courseVO);
    }


    /**
     * 根据id回显课程信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getCourseById(@PathVariable Integer id){
        Course course = courseService.getById(id);
        return new Result(200,"查询成功",course);
    }

    /**
     * 修改课程信息
     * @param courseDTO
     * @return
     */
    @PutMapping
    public Result updateCourse(@RequestBody CourseDTO courseDTO){
        courseService.updateCourse(courseDTO);
        return Result.success();
    }

    /**
     * 批量删除
     * todo:关联的相关，有东西就不能删
     * @param ids
     * @return
     */
    @DeleteMapping("/{ids}")
    public Result deleteByIds(@PathVariable String ids){
        log.info("ids:{}",ids);
        List<String> list = Arrays.asList(ids.split(","));
        courseService.removeByIds(list);
        return Result.success();
    }

    /**
     * 课程下拉列表
     * @param subject
     * @return
     */
    @GetMapping("/listselect")
    public Result listCourse(String subject){
//        QueryWrapper<Course> select = new QueryWrapper<Course>().eq("subject",subject);
//        List<Course> list = courseService.list(select);

        List<Course> list = courseService.getListbySubject(subject);
        return Result.success(list);
    }

}
